package com.cus.tom.transfer.ac;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.cus.tom.transfer.adapter.RightAdapter;
import com.cus.tom.transfer.databinding.ActRightBinding;
import com.cus.tom.transfer.frag.BaseDialogFragment;
import com.cus.tom.transfer.frag.TipDlg;
import com.cus.tom.transfer.utils.Utils;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.OnPermissionPageCallback;
import com.hjq.permissions.XXPermissions;

import java.util.List;

public class RightActivity extends BaseAct {
    ActRightBinding binding;
    RightAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActRightBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.top.setPadding(0, Utils.getStatusBarHeight(this), 0, 0);
        binding.back.setOnClickListener(v -> finish());

        adapter = new RightAdapter(this);
        binding.list.setLayoutManager(new LinearLayoutManager(this));
        binding.list.setAdapter(adapter);
    }

    @Override
    protected boolean paddingTop() {
        return false;
    }

    public void askPermission(String p, int i, boolean hasRight) {
        if (!hasRight) {
            XXPermissions.with(this).permission(p)
                    .request(new OnPermissionCallback() {
                        @Override
                        public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                            if (allGranted) {
                                adapter.notifyItemChanged(i);
                            }
                        }

                        @Override
                        public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                            if (doNotAskAgain) {
                                TipDlg dlg = new TipDlg().setMsg("权限被永久拒绝，请手动授予权限");
                                dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                                    @Override
                                    public void onDismiss(boolean isOk, Object value) {
                                        if (isOk) {
                                            XXPermissions.startPermissionActivity(
                                                    RightActivity.this, p, new OnPermissionPageCallback() {
                                                        @Override
                                                        public void onGranted() {
                                                            adapter.notifyItemChanged(i);
                                                        }

                                                        @Override
                                                        public void onDenied() {
                                                        }
                                                    });
                                        }
                                    }
                                });
                                dlg.show(RightActivity.this.getSupportFragmentManager(), "");
                            }
                        }
                    });
        } else {
            XXPermissions.startPermissionActivity(
                    RightActivity.this, p, new OnPermissionPageCallback() {
                        @Override
                        public void onGranted() {

                        }

                        @Override
                        public void onDenied() {
                            adapter.notifyItemChanged(i);
                        }
                    });
        }
    }
}
